<template>
    <div class="app-container">
        <levelbar></levelbar>
        <div class="form-box" v-loading='loading'>
            <div class="form-title">添加视频</div>
            <el-form :model="dataForm" :rules="rules" ref="dataForm" label-width="80px" class="form" label-position='right'>
                <div class="form-item">
                    <div class="form-item-title">
                        <!-- <span class="note-css"></span> -->
                        <span class="title-level">基本信息</span>
                    </div>
                    <div class="form-content">
                        <el-row :gutter="21" class="item">
                            <el-col :span="12">
                                <el-form-item label="视频名称" prop="sVideoName" :rules="rules.sVideoName">
                                    <el-input v-model="dataForm.sVideoName" :maxlength="100" placeholder="请输入视频名称" style="width:90%"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="21"  class="item">
                            <el-col :span="12">
                                <el-form-item label="视频封面" prop="sCoverUrl" :rules="rules.sCoverUrl">
                                    <fileUpload @onSuccess="imgSuccessFn" @remove="fileImgRemove" :file-list-show.sync='imgTrue' :file-list.sync="arrList" :imgSize='0.09765625'>
                                        <div class="el-upload-new el-upload-picture-new">
                                            <div class="up-container el-upload">
                                                <i class="el-icon-plus"></i>
                                            </div>
                                        </div>
                                    </fileUpload>
                                    <p class="code">建议尺寸300*200 或 3:2，JPG、PNG格式,图片小于100K</p>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="21"  class="item">
                            <el-col :span="12">
                                <el-form-item label="视频上传" prop="sVideoUrl" :rules="rules.sVideoUrl">
                                    <fileUpload  
                                    @remove="fileFileRemove" 
                                    :file-list.sync="fileOptions.fileList" 
                                    :file-list-show.sync="fileOptions.fileListShow" 
                                    :list-type="fileOptions.listType"
                                    :up-file-type="fileOptions.upFileType"
                                    :accept-list="fileOptions.accept"
                                    @onSuccess="fileSuccessFn"
                                    >
                                        <el-button type="default">上传附件</el-button>
                                        <!-- <div slot="tip" class="tip">支持100M以内的word、excel、ppt、rar、zip文档类型</div> -->
                                    </fileUpload >
                                    <p class="code">仅支持MP4格式H.264 视频编码的视频,视频小于500M</p>
                                    <div v-if="dataForm.sVideoUrl">
                                        <video style="max-width:600px;max-height:650px" controls :autoplay="false" buffered :src="dataForm.sVideoUrl"></video>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="21"  class="item">
                            <el-col :span="22">
                                <el-form-item label="视频介绍" prop="sDesc" :rules="rules.sDesc">
                                    <Tinymce :toolbar="toolbar" :height="260" ref="dataForm.sDesc" :id="tinymceId" v-model="dataForm.sDesc"></Tinymce>
                                    <p>编辑器内容输入不要超过6000字符！</p>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                </div>
                <div class="form-item">
                    <div class="form-item-title">
                        <!-- <span class="note-css"></span> -->
                        <span class="title-level">上架信息</span>
                    </div>
                    <div class="form-content">
                        <el-row :gutter="21">
                            <el-col :span="12">
                                <el-form-item label="付费形式" prop="sPayType" :rules="rules.sPayType">
                                    <el-radio class="radio" v-model="dataForm.sPayType" label="0">免费</el-radio>
                                    <el-radio class="radio" v-model="dataForm.sPayType" label="1">优惠</el-radio>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="41" v-if="dataForm.sPayType == '1'">
                            <div class='original-price'>
                                <el-col :span="5">
                                    <el-form-item label="原价" label-width="60px" prop="sOriginalPrice" :rules="rules.sOriginalPrice">
                                        <el-input v-model="dataForm.sOriginalPrice" style="width:100%;"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="5">
                                    <el-form-item label="现价" label-width="60px">
                                        <el-input :disabled="true" v-model="dataForm.sNowPrice" style="width:100%;"></el-input>
                                    </el-form-item>
                                    <!-- <el-form-item label="现价" label-width="60px" prop="sNowPrice" :rules="rules.sNowPrice">
                                        <el-input :disabled="true" v-model.number="dataForm.sNowPrice" style="width:100%;"></el-input>
                                    </el-form-item> -->
                                </el-col>
                            </div>
                        </el-row>
                        <el-row :gutter="21">
                            <el-col :span="24">
                                <el-form-item label="所属分类" prop="fTypeId" :rules="rules.fTypeId">
                                    <el-radio-group v-model="dataForm.fTypeId">
                                        <el-radio v-for="item in dataForm.fTypeIdList" :label="item.pId" :key="item.pId">{{item.sName}}</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="21" class="item">
                            <el-col :span="12">
                                <el-form-item label="权重" prop="sPriority" :rules="rules.sPriority">
                                    <el-popover
                                    placement="top-start"
                                    trigger="focus" 
                                    content="请输入不超出5位的数字。">
                                        <el-input v-model="dataForm.sPriority" slot="reference" :maxlength="5" style="width:150px;" placeholder="请输入权重"></el-input>
                                    </el-popover>
                                </el-form-item>
                                <!-- <el-form-item label=" 权重">
                                   <el-input-number :min="1" :max="9999" v-model="dataForm.sPriority" style="width:150px;"></el-input-number>
                                </el-form-item> -->
                            </el-col>
                        </el-row>
                    </div>
                </div>
                <div class="form-item">
                    <el-row>
                        <el-col :span="24">
                            <el-form-item>
                                <div class="footer">
                                    <el-button @click="resetForm('dataForm')">取消</el-button>
                                    <el-button type="primary" @click="submitForm('dataForm')">确认</el-button>
                                </div>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
    import { isEmpty } from 'utils/validate';
    import Levelbar from '../../../layout/Levelbar';
    import fileUpload from 'components/Upload/fileUpload';
    import Tinymce from 'components/Tinymce';
    import {saveScholar, listAdvertByCondition} from 'api/eStudyBug';
    import { mapGetters} from 'vuex';
    export default {
        name: 'addstudybugVideo',
        components: { Levelbar, fileUpload, Tinymce },
        data() {
            const validateEmpty = (rule, value, callback) => {
                if (!isEmpty(value)) {
                    callback(new Error('内容中不能存在空格'));
                } else {
                    callback();
                }
            };
            return {
                imgTrue: true,
                loading: false,
                fileOptions: {
                    upFileType: 'video',
                    fileListShow: true,
                    fileList: []
                },
                tinymceId: 'tinymceId',
                dataForm: {
                    fileName: '',
                    sVideoName: '',
                    sCoverUrl: '',
                    videoUrl: '',
                    sDesc: '',
                    sPayType: '0',
                    sOriginalPrice: '',
                    sNowPrice: '0.00',
                    fTypeId: '',
                    fTypeIdList: [],
                    sPriority: ''
                },
                fTypeId: '',
                arrList: [],
                rules: {
                    sVideoName: [
                        { required: true, message: '视频名称不能为空！', trigger: 'blur' },
                        { required: true, trigger: 'blur', validator: validateEmpty },
                        { max: 14, min: 1, message: '长度在14个字符以内', trigger: 'blur' }
                    ],
                    sCoverUrl: [
                        { required: true, message: '请上传图片', trigger: 'blur' }
                    ],
                    sVideoUrl: [
                        { required: true, message: '请上传视频', trigger: 'blur' }
                    ],
                    sDesc: [
                        { required: true, message: '请输入视频介绍', trigger: 'change' }
                    ],
                    sPayType: [
                        { required: true, message: '请选择', trigger: 'blur' }
                    ],
                    sOriginalPrice: [
                        { required: true, message: '内容不能为空'},
                        { pattern: /^(([1-9]\d*)|(([0]\.\d{1,2})|([1-9]\d*\.\d{1,2})))$/, message: '请输入1~1000两位小数的数字'},
                        { validator: (rule, value, callback) => {
                            if (Number(value)>1000) {
                                callback(new Error('请输入1~1000两位小数的数字'));
                            } else {
                                callback();
                            }
                        }, trigger: 'blur' }

                    ],
                    sNowPrice: [
                        { required: true, message: '内容不能为空'},
                        { type: 'number', message: '内容必须为数字值'}
                    ],
                    fTypeId: [
                        { required: true, message: '请选择所属分类', trigger: 'change' }
                    ],
                    sPriority: [
                        { pattern: /^\d{0,5}$/, message: '请输入不超出5位的数字', trigger: 'blur' }
                    ]
                },
                toolbar: ['removeformat undo redo |  bullist numlist | outdent indent | forecolor ', 'bold italic blockquote | h2 p | alignleft aligncenter alignright']

            };
        },
        computed: {
            ...mapGetters([
                'uid'
            ])
        },
        created() {
            listAdvertByCondition().then(data => {
                this.dataForm.fTypeIdList = data.data.content
            })
        },
        methods: {
            // 移除图片
            fileImgRemove(file) {
                this.dataForm.sCoverUrl = ''; 
            },

            // 上传图片成功后获取图片路径
            imgSuccessFn(obj) {
                this.dataForm.sCoverUrl = `${obj.imgUrl}?imageView2/2/w/300/h/200`;
                this.$refs.dataForm.validateField('sCoverUrl');
            },
            // 移除文件
            fileFileRemove(file) {
                this.dataForm.sVideoUrl = '';
                this.fileOptions.fileList = [];
            },

            // 上传文件成功后获取文件路径
            fileSuccessFn(obj) {
                this.$message('上传成功！');
                this.dataForm.sVideoUrl = obj.imgUrl;
                this.$refs.dataForm.validateField('sVideoUrl');
            },   

            // 获取上传文件名
            getFileName(file) {
                this.dataForm.fileName = file.name;
            },
            submitForm(dataForm) {
                this.$refs[dataForm].validate(valid => {
                    let tempHtml = window.tinymce.get('tinymceId').getContent({format: 'raw'});
                    if (tempHtml.indexOf('img') === -1) {
                        if (!window.tinymce.get('tinymceId').getContent({format: 'text'}).trim()) {
                            this.$message.error('请输入视频介绍');
                            return false;
                        }
                    }
                    if (window.tinymce.get('tinymceId').getContent({format: 'text'}).trim().length > 6000) {
                        this.$message.error('视频介绍字数过多');
                        return false;
                    }
                    if (valid) {
                        if (this.dataForm.sVideoUrl !== '') {
                            this.$confirm('确认提交?', ' ', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: ' '
                            }).then(() => {
                                this.loading = true;
                                saveScholar(
                                    this.dataForm.sVideoName,
                                    this.dataForm.sCoverUrl,
                                    this.dataForm.sVideoUrl,
                                    this.dataForm.sDesc,
                                    this.dataForm.sPayType,
                                    !this.dataForm.sOriginalPrice ? '' : ((this.dataForm.sOriginalPrice*1).toFixed(2))+'',
                                    this.dataForm.sNowPrice,
                                    this.dataForm.fTypeId,
                                    this.dataForm.sPriority
                                ).then(response => {
                                    this.loading = false;
                                    this.$message.success(response.data.errorMsg);
                                    this.$router.push({ path: '/eStudybug/eStudybug' });
                                    this.$store.dispatch('UpdateCache');
                                }).catch(Error => {
                                    this.loading = false;
                                });
                            }).catch(() => {
                                this.$message({
                                    type: 'info',
                                    message: '已取消'
                                });          
                            });
                            
                        } else {
                            this.$message('视频上传中...');
                        }
                    } else {
                        return false;
                    }
                });
            },
            resetForm(dataForm) {
                this.$router.push({ path: '/eStudybug/eStudybug' });
            }
        }
    };
</script>

<style lang="scss" scoped>
    .code{
        font-size: 14px;
        color: #ccc;
    }
    font{
        color: red
    }
    .app-container {
        position: relative;
        padding: 20px 20px 10px;
        $bgcolor: #fff;

        @mixin showTitle() {
            background: $bgcolor;
            box-shadow:  0px 0px 5px 0px rgba(0, 0, 0, 0.1);
            -webkit-box-shadow:  0px 0px 5px 0px rgba(0, 0, 0, 0.1);
        }
        .form-box {
            padding: 0px 50px;
            @include showTitle();
            .form-title {
                padding-top:8px;
                text-align: center;
                font-size: 18px;
                color: #333;
                line-height: 72px;
                font-weight: bold;
                font-family: Microsoft YaHei;
                border-bottom: 1px solid #ddd;
            }
            .form {
                .form-item {
                    padding-bottom: 40px;
                    .form-item-title {
                        margin: 40px 0;
                        font-size: 14px;
                        color: #333;
                        line-height: 14px;
                        border-left: 4px solid #ff5e2c;
                        text-indent: 16px;
                        .note-css {
                            vertical-align: middle;
                            display: inline-block;
                            width: 4px;
                            height: 14px;
                            margin-right: 16px;
                            border-left: 3px solid #ff5e2c;
                        }
                    }
                    .form-content {
                        margin: 0 8px;
                        .original-price {
                            padding-left:102px;
                            // overflow:hidden;
                            margin-bottom: 20px;
                        }
                        .subject-radio {
                            padding-left:82px;
                        }
                        .item {
                            margin-bottom: 20px;
                        }
                    }
                    .item {
                        margin-bottom: 20px;
                    }
                    .back {
                        margin-top: 20px;
                    }
                    .footer {
                        text-align: center;
                        margin-top: 40px;
                    }
                    .title-way {
                        padding-right: 15px;
                    }
                    .control {
                        text-align: right;
                    }
                }
            }
        }
    }
</style>